<?php
/**
 * 
 * @author codetaste
 *
 */
class FollowAction extends HomeCommonAction {
    public function index(){
    	$map_c['uid'] = Session::getLocal('id');
    	$map_c['status'] = 1;
    	$sp_list = $this->_list(M("BaseFollow"),$map_c);
    	foreach($sp_list as $k=>$v){
    		$sp_list[$k]["info"] = $this->getInfo(M("BaseCommodity"),$v['commodity_id']);
    		$sp_list[$k]['brand_info'] = $this->getInfo(M("BaseBrand"), $sp_list[$k]["info"]['brand_id']);
    		$sp_list[$k]['category_info'] = $this->getInfo(M("BaseCategory"), $sp_list[$k]["info"]['category_id']);
    		$sp_list[$k]['tiezi'] = $this->getShare($v['commodity_id'],$v['tiezi_num']);
    		$sp_list[$k]['sp_count'] = $this->getCount(M("BaseFollow"), array("commodity_id"=>$v['commodity_id'])); //商品关注数
    		foreach($sp_list[$k]['tiezi'] as $key=>$val){
//    			echo $key;
    			$sp_list[$k]['tiezi'][$key]['content_str'] = msubstr($val['content'], 0, 153)."...";
    		}
    	}
    	$this->assign("sp_list",$sp_list);
    	$this->display();
    }
    
    public function getShare($id,$tiezi_num){
    	$count = M("Share")->where("commodity_id='$id'")->count();
    	$num = number_format($count-$tiezi_num);
//    	$num = $count;
    	$list = M("Share")->where("commodity_id='$id'")->order("id desc")->limit("0,$num")->select();
    	return $list;
    }
    
    public function adds() {
    	$result = $this->check("add",$_REQUEST['id']);
    	if(!$result){
    		showMsg("已关注");
    	}
    	$result = D("Follow")->add($_REQUEST['id']);
    	if($result){
    		$this->success("成功关注商品");
    	}else{
    		$this->error("关注商品失败");
    	}
    }
    
    public function notShow() {
    	$result = D("Follow")->notShow($_REQUEST['id']);
    	if($result){
    		$data['success'] = 1;
    		$data['message'] = "成功取消左侧显示";
    	}else{
    		$data['success'] = 0;
    		$data['message'] = "数据异常";
    	}
    	die(json_encode($data));
    }
    
    public function dels() {
    	$result = D("Follow")->del($_REQUEST['id']);
    	if($result){
    		$this->success("成功取消关注");
    	}else{
    		$this->success("取消关注失败");
    	}
    }
	
	public function check($act,$id){
		if($act == 'add'){
			$map['commodity_id'] = $id;
			$map['uid'] = Session::getLocal('id');
			$map['status'] = 1;
			$info = $this->getInfo(M("BaseFollow"),'',$map);
			return $info['id']?false:true;
		}
	}
    
}
?>
